package com.gy.assistant.ui.chat.room;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;

import java.util.ArrayList;
import java.util.HashMap;

import im.zego.zim.callback.ZIMRoomAttributesOperatedCallback;
import im.zego.zim.entity.ZIMError;
import im.zego.zim.entity.ZIMRoomAttributesSetConfig;
import im.zego.zim.enums.ZIMErrorCode;
import com.gy.assistant.R;
import com.gy.assistant.SDKManager;
import com.gy.assistant.ui.BaseActivity;

public class ZIMRoomNoticeUpdateActivity extends BaseActivity {

    private EditText roomInfoEditText;

    public static void actionStart(Activity activity, String roomID, String roomNotice) {
        Intent intent = new Intent(activity, ZIMRoomNoticeUpdateActivity.class);
        intent.putExtra("roomID", roomID);
        intent.putExtra("roomNotice", roomNotice);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_room_info_update);

        TextView roomInfoText = findViewById(R.id.group_info_text);
        TextView roomInfoTitle = findViewById(R.id.group_info_title);

        roomInfoText.setText("房间信息");
        roomInfoTitle.setText("修改房间公告");

        roomInfoEditText = findViewById(R.id.group_info);
        roomInfoEditText.setText(getIntent().getStringExtra("roomNotice"));

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void onUpdateGroupInfo(View view) {
        String info = roomInfoEditText.getText().toString();
        String roomID = getIntent().getStringExtra("roomID");
        HashMap<String, String> attrMap = new HashMap<>();
        attrMap.put(ZIMRoomMemberActivity.ROOM_NOTICE_ATTR, info);

        SDKManager.sharedInstance().setRoomAttributes(attrMap, roomID, new ZIMRoomAttributesSetConfig(), new ZIMRoomAttributesOperatedCallback() {
            @Override
            public void onRoomAttributesOperated(String roomID, ArrayList<String> errorKeys, ZIMError errorInfo) {
                
                if (errorInfo.code != ZIMErrorCode.SUCCESS) {
                    Toast.makeText(ZIMRoomNoticeUpdateActivity.this, "Failed to update room notice error: " + errorInfo.code.name(), Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(ZIMRoomNoticeUpdateActivity.this, "Update success", Toast.LENGTH_LONG).show();
                }
            }
        });
    }

}
